package com.salim.leetcode.$7;

/**
 * Created with IntelliJ IDEA.
 * User: clevo
 * Date: 15-9-14
 * Time: 上午11:17
 * To change this template use File | Settings | File Templates.
 */
public class ReverseInteger {
    public int reverse(int x) {
        //-2147483638
        int negative = 1;
        if(x<0){
            negative = -1;
            x=x*negative;
        }
        int xtemp = x;
        int result = 0;
        int pow = 1;
        int maxi= 0;
        while(xtemp>0){
            maxi++;
            if(maxi>=10){
                break;
            }
            /*pow*=10;*/
            xtemp/=10;
        }
        while(x>0){
            int index = x%10;
            int powTemp=index;
            int count = maxi-1;
            int lastpowTemp = 0;
            while(count>0){
                powTemp*=10;
                //越界
                if(powTemp<0|| lastpowTemp>powTemp){
                    return 0;
                }
                lastpowTemp = powTemp;
                count--;
            }
            maxi--;
            result+=powTemp;
            if(result<0){
                return 0;
            }
            x/=10;
            pow/=10;
        }

        result*=negative;
        return result;
    }

    public static void main(String[] args){
        ReverseInteger ri = new ReverseInteger();
/*        int negative = -1234;
        System.out.println(ri.reverse(negative));
        int min = Integer.MIN_VALUE;
        System.out.println(ri.reverse(min));
        int max = Integer.MAX_VALUE;
        System.out.println(ri.reverse(max));
        int zero = 0;
        System.out.println(ri.reverse(zero));*/
        int ten = 10;
        System.out.println(ri.reverse(ten));
        int a12313 = 1534236469;
        System.out.println(ri.reverse(a12313));
    }
}
